//
//  SliderImageViewController.m
//  clublbiza
//
//  Created by WANG TOM on 8/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SliderImageViewController.h"
#import "TMDataTable.h"
#import "clublbizaAppDelegate.h"
#import "Slider_image.h"
#import "Resource.h"
#import "CycleScrollView.h"

@interface SliderImageViewController ()

@end

@implementation SliderImageViewController
@synthesize cycleView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(refreshSliderImageDataList)
                                                 name: @"currentSelectedMenuTabIndexChanged"
                                               object: nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
     [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [self.cycleView release];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
- (void) refreshSliderImageDataList
{    
    AppMain *appMain = [AppMain shareInstance];
    int menutab_index = appMain.currentSelectedMenuTabIndex;
    if ( menutab_index < 3 ) {
        return;
    }
    


    
    NSString *sliderImageType = nil;
    if ( menutab_index == 3) {
        sliderImageType = @"events";
    } else if ( menutab_index == 4 ) {
        sliderImageType = @"advertisement";
    }
    
    if ( sliderImageType == nil ) {
        return;
    }
    clublbizaAppDelegate *appDelegate = (clublbizaAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    TMDataTable *dtSliderImage = [[TMDataTable alloc] initWithTablename:@"Slider_image" withPKName:@"uuid" contextProvider:appDelegate];
    
    NSString *condition = [NSString stringWithFormat:@"type='%@' AND active=1", sliderImageType ];
    NSArray *sliderImageAll = [dtSliderImage findAll:condition withSort:@"order"];
    [dtSliderImage release];
    
    //Remove all item views
    NSArray *subviews = [[self.view subviews] copy];
    for (UIView *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];
    
    
	// Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *picArray = [[NSMutableArray alloc] init];
    TMDataTable *dtResource = [[TMDataTable alloc] initWithTablename:@"Resource" withPKName:@"uuid" contextProvider:appDelegate];
    for(  Slider_image *sliderImageData in sliderImageAll) 
    {
        
        Resource *resourceData = (Resource*)[dtResource find:sliderImageData.resource_uuid];
         NSString *sliderImageFilePath =  [[AppMain resourceFolderPath] stringByAppendingFormat:@"/%@",resourceData.path];
        [picArray addObject:[UIImage imageWithContentsOfFile:sliderImageFilePath]];
    }

    
    //    CFShow(picArray);
    self.cycleView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 720)
                                                     cycleDirection:CycleDirectionLandscape
                                                           pictures:picArray];
    self.cycleView.delegate = self;
    [self.view addSubview:self.cycleView];
    [self.cycleView release];
    
    [picArray release];
    
    //right button
    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRight.frame = CGRectMake(970,300,48,48);
      UIImage *right_image = [UIImage imageNamed:@"scroll_righ.png"];
    [btnRight setBackgroundImage:right_image forState:UIControlStateNormal];
    btnRight.alpha = 0.3;
    [btnRight addTarget:self action:@selector(scrollNextImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnRight];

    
    
    //left button
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *left_image = [UIImage imageNamed:@"scroll_left.png"];
    [btnLeft setBackgroundImage:left_image forState:UIControlStateNormal];
    btnLeft.frame = CGRectMake(30,300,48,48);
    btnLeft.alpha = 0.3;
    [btnLeft addTarget:self action:@selector(scrollPreviousImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLeft];
    
       
   }
- (void) scrollNextImage
{
    [self.cycleView nextImage];
}
- (void) scrollPreviousImage
{
     [self.cycleView previousImage];
}

@end
